<?php

/**
 * Form input where value must fit in specified range
 * @author shenlon
 */
class FormNumberRangeInput extends FormInput {

    /**
     * Default constructor
     * @param String $name
     * @param String $required
     * @param Float $min
     * @param Float $max
     * @param Float $step
     * @param Array $options 
     */
    public function __construct($name, $required = false, $min = 0, $max = 100000, $step = 1, $options = null) {
        parent::__construct($name, $required, 'number', 'numeric');
        $this->optionalData['min'] = $min;
        $this->optionalData['max'] = $max;
        $this->optionalData['step'] = $step;
        if ($options != null)
            foreach ($options as $key => $value) {
                if (property_exists($this, $key))
                    $this->$key = $value;
                else
                    $this->optionalData[$key] = $value;
            }
    }

    /**
     * Validates data if it fits the range and step
     * @param Float $data
     * @return Array 
     */
    public function validateData($data) {
        $errorMsg = "Wartość $data nie mieści się w przedziale {$this->optionalData['min']} i {$this->optionalData['max']}
        lub nie dzieli się przez {$this->optionalData['step']}";
        $errors = parent::validateData($data);
        if ($data != '') {
            if ($data < $this->optionalData['min'] || $data > $this->optionalData['max'] ||
                    fmod($data / $this->optionalData['step'], 1.0) != 0) {
                if (!is_array($errors))
                    $errors = array($errorMsg);
                else
                    $errors[] = $errorMsg;
            }
        }
        return $errors;
    }

}

?>
